package tech.aistar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.UserRelation;
import tech.aistar.mapper.UserRelationMapper;
import tech.aistar.service.IUserInfoService;
import tech.aistar.service.IUserRelationService;
import tech.aistar.util.IdWorker;

import java.util.*;

@Service
@Transactional
public class UserRelationServiceImpl implements IUserRelationService {
    @Autowired
    private UserRelationMapper userRelationMapper;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IdWorker idWorker;

    @Override
    public List<UserRelation> findFansNum(Integer id) {
        List<UserRelation>userRelations = userRelationMapper.findFansNum(id);
        return userRelations;
    }


    @Override
    public List<UserRelation> findFoucNum(Integer userByid) {
        List<UserRelation> userRelations = userRelationMapper.findFoucNum(userByid);
        return userRelations;
    }


    @Override
    //删除的同时 数量减少
    public void delFouc(Integer id, Integer userByid) {
        userRelationMapper.delFouc(id,userByid);
        //先获取数量
        UserInfo u = userInfoService.findById(userByid);
        userInfoService.calFouc(u.getId(),u.getFoucNum()-1);
        UserInfo u1 = userInfoService.findById(id);
        userInfoService.calFans(u1.getId(),u1.getFansNum()-1);
    }

    @Override
    //添加的时候 数量增加
    public void addShip(UserRelation userRelation) {
        userRelation.setReId(idWorker.nextId());
        userRelationMapper.addShip(userRelation);
        UserInfo u = userInfoService.findById(userRelation.getId());
        userInfoService.calFans(u.getId(),u.getFansNum()+1);
//        int s = 10/0;
//        System.out.println(s);
        UserInfo u1 = userInfoService.findById(userRelation.getUserByid());
        userInfoService.calFouc(u1.getId(),u1.getFoucNum()+1);
    }

    @Override
    public Long findShip(Integer id, Integer user_byid, Integer status) {
        Long reId = userRelationMapper.findShip(id, user_byid, status);
        return reId;
    }

    @Override
    public void chanageShip(Integer id, Integer userId) {
        UserInfo u = userInfoService.findById(id);
        userInfoService.calFans(u.getId(),u.getFansNum()+1);

        userRelationMapper.chanageShip(id, userId);

        UserInfo u1 = userInfoService.findById(userId);
        userInfoService.calFouc(u1.getId(),u1.getFoucNum()+1);
    }


    //根据userByid 查出它关注的人
    @Override
    public List<UserInfo> findFocuInfo(Integer userByid) {
        List<UserRelation> fList = userRelationMapper.findFoucNum(userByid);
        Iterator iter = fList.iterator();
        //Map<Integer,List<UserInfo>> remap = new HashMap<>();
        List<UserInfo> list = new ArrayList<>();
        while(iter.hasNext()){
            UserRelation u = (UserRelation) iter.next();
            UserInfo info = userInfoService.findById(u.getId());
            list.add(info);
        }
//        System.out.println("从数据库获取数据:"+list);
//        System.out.println(list.size()+"***********************************");
        return list;

    }


    @Override
    public List<UserInfo> findFansInfo(Integer id) {
        List<UserRelation> fList = userRelationMapper.findFansNum(id);
        Iterator iter = fList.iterator();
        //Map<Integer,List<UserInfo>> remap = new HashMap<>();
        List<UserInfo> list = new ArrayList<>();
        while(iter.hasNext()){
            UserRelation u = (UserRelation) iter.next();
            UserInfo info = userInfoService.findById(u.getUserByid());
            list.add(info);
        }
//        remap.put(userByid,list);
        return list;
    }


    //中间分页
//    @Override
//    public PageInfo<UserRelation> findFansNum(Integer id, Integer pageNum, Integer pageSize) {
//        return null;
//    }


    //中间分页
    @Override
    public Map<PageInfo<UserRelation>,List<UserInfo>> findFoucNum(Integer userByid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //先查所有在分页显示
        List<UserRelation> relations = findFoucNum(userByid);
        System.out.println(relations.size());
        PageInfo<UserRelation> focupage = new PageInfo<>(relations);
        System.out.println(focupage.getPages()+"####################");

        Iterator iter = focupage.getList().iterator();
        //Map<Integer,List<UserInfo>> remap = new HashMap<>();
        List<UserInfo> list = new ArrayList<>();
        while(iter.hasNext()){
            UserRelation u = (UserRelation) iter.next();
            UserInfo info = userInfoService.findById(u.getId());
            list.add(info);
        }
        Map<PageInfo<UserRelation>,List<UserInfo>> map = new HashMap<>();
        map.put(focupage,list);
        return map;

    }

    @Override
    public Map<PageInfo<UserRelation>, List<UserInfo>> findFansNum(Integer id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //先查所有在分页显示
        List<UserRelation> relations = findFansNum(id);
        System.out.println(relations.size());
        PageInfo<UserRelation> fanspage = new PageInfo<>(relations);
        System.out.println(fanspage.getPages()+"####################");

        Iterator iter = fanspage.getList().iterator();
        //Map<Integer,List<UserInfo>> remap = new HashMap<>();
        List<UserInfo> list = new ArrayList<>();
        while(iter.hasNext()){
            UserRelation u = (UserRelation) iter.next();
            UserInfo info = userInfoService.findById(u.getUserByid());
            list.add(info);
        }
        Map<PageInfo<UserRelation>,List<UserInfo>> map = new HashMap<>();
        map.put(fanspage,list);
        return map;
    }

    @Override
    public Boolean bothShip(Integer id, Integer userByid) {
        Long reId01 = userRelationMapper.bothShip(id,userByid);
        Long reId02 = userRelationMapper.bothShip(userByid,id);
        System.out.println(reId01);
        System.out.println("=====");
        System.out.println(reId02);
        if(null==reId01 || null==reId02)
            return false;
        return true;
    }

}
